package com.dnjn.happiness.survey.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.happiness.common.core.constant.Constants;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.survey.domain.CheckAnswer;
import com.dnjn.happiness.survey.domain.CheckItem;
import com.dnjn.happiness.survey.domain.vo.CheckAnswerVO;
import com.dnjn.happiness.survey.mapper.CheckAnswerMapper;
import com.dnjn.happiness.survey.service.ICheckAnswerService;
import com.dnjn.happiness.survey.service.ICheckItemService;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.core.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * 考核答案Service业务层处理
 *
 * @author happiness
 * @date 2024-04-02
 */
@Service
public class CheckAnswerServiceImpl extends ServiceImpl<CheckAnswerMapper, CheckAnswer> implements ICheckAnswerService {

    @Autowired
    private ICheckItemService checkItemService;


    @Override
    public void saveList(CheckAnswerVO domain) {

        List<CheckAnswerVO> voList = domain.getVoList();


        voList.stream().forEach(vo -> {
            Assert.notNull(vo.getCheckId(), "checkId不能为空");
            Assert.notNull(vo.getCheckItemId(), "checkItemId不能为空");
            CheckAnswer checkAnswer = new CheckAnswer();
            BeanUtils.copyPropsIgnoreNull(vo, checkAnswer);

            checkAnswer.setCreateBy(SecurityUtils.getUsername());
            checkAnswer.setUserId((SecurityUtils.getUserId()));

            baseMapper.insert(checkAnswer);

        });


        Integer checkItemScore = domain.getCheckItemScore();//总分
        String checkItemResult = domain.getCheckItemResult();//总结果
        Long superiorCheckScore = domain.getSuperiorCheckScore();
        String superiorCheckResult = domain.getSuperiorCheckResult();//上级结果
        Long subordinateCheckScore = domain.getSubordinateCheckScore();//下级分数
        String subordinateCheckResult = domain.getSubordinateCheckResult();//下级结果


        Long userId = SecurityUtils.getUserId();

        CheckItem checkItem = checkItemService.lambdaQuery().eq(CheckItem::getCheckItemId, domain.getCheckItemId()).eq(CheckItem::getDeleted, Constants.UNDELETED).one();
        if (null != checkItem) {
            if (checkItem.getCheckType().equals(Constants.TIKU_CATEGORY_STAFF)) {//员工改上级的 和总结果
                updateSuperiorCheckScoreItem(checkItem.getCheckItemId(), superiorCheckScore, superiorCheckResult);
                updataCheckStatus(checkItem.getCheckItemId(), checkItemScore, checkItemResult);
            }
            if (checkItem.getCheckType().equals(Constants.TIKU_CATEGORY_MANAGER)) {
                Boolean sup = false;
                Boolean sub = false;

                if (!StringUtils.isEmpty(superiorCheckResult)) {
                    sup = updateSuperiorCheckScoreItem(checkItem.getCheckItemId(), superiorCheckScore, superiorCheckResult);
                }
                if (!StringUtils.isEmpty(subordinateCheckResult)) {
                    sub = updateSubordinateCheckScoreItem(checkItem.getCheckItemId(), subordinateCheckScore, subordinateCheckResult);
                }
                CheckItem one = checkItemService.lambdaQuery().eq(CheckItem::getCheckItemId, domain.getCheckItemId()).eq(CheckItem::getDeleted, Constants.UNDELETED).one();
                if (!StringUtils.isEmpty(one.getSuperiorStatus()) && !StringUtils.isEmpty(one.getSubordinateStatus())) {
                    updataCheckStatus(checkItem.getCheckItemId(), checkItemScore, checkItemResult);
                }
                if (Objects.isNull(one.getSuperiorUserId()) && !StringUtils.isEmpty(one.getSubordinateStatus())) {//没有上级,下级状态已经不为null(默认是null)
                    updataCheckStatus(checkItem.getCheckItemId(), checkItemScore, checkItemResult);
                }


            }

        }


    }

    private void updataCheckStatus(Long checkItemId, Integer checkItemScore, String checkItemResult) {
        checkItemService.lambdaUpdate()
                .eq(CheckItem::getCheckItemId, checkItemId)
                .set(CheckItem::getCheckStatus, 'Y')
                .set(CheckItem::getCheckItemScore, checkItemScore)
                .set(CheckItem::getCheckItemResult, checkItemResult)
                .update();
    }




    @Override
    public List<CheckAnswer> getAnswerByCheckItemId(Long checkItemId) {

        return baseMapper.getAnswerByCheckItemId(checkItemId);
    }

    @Override
    public AjaxResult getAnswerByCheckItemIdAndUserId(Long checkItemId, Long superiorUserId, Long subordinateUserId) {
        ArrayList<CheckAnswer> checkAnswerArrayList = new ArrayList<>();
        CheckItem one = checkItemService.lambdaQuery().eq(CheckItem::getCheckItemId, checkItemId).eq(CheckItem::getDeleted, Constants.UNDELETED).one();

        AjaxResult ajaxResult = new AjaxResult();
        //上级 下级
        if (null != one && null != one.getSuperiorUserId() && one.getSuperiorUserId().equals(superiorUserId)) {
            List<CheckAnswer> checkAnswers = baseMapper.getSupAnswer(checkItemId, superiorUserId, null);

            ajaxResult.put("上级问卷", checkAnswers);
        }
        if (null != one && null != one.getSubordinateUserId() && one.getSubordinateUserId().equals(subordinateUserId)) {
            List<CheckAnswer> checkAnswers = baseMapper.getSupAnswer(checkItemId, null, subordinateUserId);
            ajaxResult.put("下级问卷", checkAnswers);
        }
        return ajaxResult;
    }


    //下级
    private boolean updateSubordinateCheckScoreItem(Long checkItemId, Long subordinateCheckScore, String subordinateCheckResult) {
        return checkItemService.lambdaUpdate()
                .eq(CheckItem::getCheckItemId, checkItemId)
                .eq(CheckItem::getDeleted, Constants.UNDELETED)
                .set(CheckItem::getSubordinateCheckScore, subordinateCheckScore)
                .set(CheckItem::getSubordinateCheckResult, subordinateCheckResult)
                .set(CheckItem::getSubordinateStatus, 'Y')
                .update();

    }


    //上级
    private boolean updateSuperiorCheckScoreItem(Long checkItemId, Long superiorCheckScore, String superiorCheckResult) {
        return checkItemService.lambdaUpdate()
                .eq(CheckItem::getCheckItemId, checkItemId)
                .eq(CheckItem::getDeleted, Constants.UNDELETED)
                .set(CheckItem::getSuperiorCheckScore, superiorCheckScore)
                .set(CheckItem::getSuperiorCheckResult, superiorCheckResult)
                .set(CheckItem::getSuperiorStatus, 'Y').update();

    }
}
